/**
 * @fileOverview satellite
 * @author huangtonger@aliyun.com
 */

const Base = require('../../../../simpleBase');
const Util = require('../../util/');

class Satellite extends Base {
  getDefaultCfg() {
    return {
      /**
       * @type  {string} name - satellite name
       */
      name: '',

      /**
       * @type  {function} render - satellite render
       * @param {object} diagram - diagram
       * @return {string|dom} dom or html
       */
      render(/* diagram */) {
        return '<button>satellite</button>';
      },

      /**
       * @type  {function} render - satellite render
       * @param {object} dom - dom element
       * @param {object} diagram - diagram
       * @return {array} events
       */
      bindEvent(/* dom, diagram */) {
        return [];
      }
    };
  }
  _renderDOM() {
    const dom = Util.createDOM(this.render(this.diagram));
    this.dom = dom;
    dom.isSatellite = true;
    return dom;
  }
  getDOM() {
    return this.dom || this._renderDOM();
  }
  init() {
    const dom = this.getDOM();
    const diagram = this.diagram;
    const graph = diagram.getGraph();
    const graphContainer = graph.getGraphContainer();
    if (dom) {
      dom.css({
        position: 'absolute',
        visibility: 'hidden'
      });
      graphContainer.appendChild(dom);
      dom && this.bindEvent(dom, this.diagram);
    }
  }
  enable() {
    return true;
  }
  show() {
    const dom = this.getDOM();
    dom.show();
  }
  hide() {
    const dom = this.getDOM();
    dom.hide();
  }
  isVisible() {
    const dom = this.getDOM();
    return dom.style.visibility !== 'hidden';
  }
  destroy() {
    const { events } = this;
    if (events) {
      events.forEach(event => {
        event.remove();
      });
    }
  }
}

module.exports = Satellite;
